package main

import (
	"fmt"
	"sync"
)

var (
	myMap = make(map[int]int, 10)
	lock  sync.Mutex
)

func test(n int) {
	res := 1
	for i := 1; i <= n; i++ {
		res *= n
	}

	lock.Lock()
	myMap[n] = res
	lock.Unlock()
}
func testPainc() {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println("testPanic错误", err)
		}
	}()
	var m map[int]int
	m[0] = 1
	a := 1 / 0
	fmt.Println(a)
}
func main() {
	//cpuNum := runtime.NumCPU()
	//fmt.Println(cpuNum)
	//
	//runtime.GOMAXPROCS(cpuNum - 1)
	//fmt.Println("cpu数量")
	////1-200阶乘
	//for i := 1; i <= 200; i++ {
	//	go test(i)
	//}
	//time.Sleep(time.Second * 10)
	//lock.Lock()
	//for i, v := range myMap {
	//	fmt.Printf("map[%d]=%d\n", i, v)
	//}
	//lock.Unlock()
	////默认管道是双向
	////声明为只写
	//var c2 chan<- int
	//c2 = make(chan int, 3)
	//c2 <- 12
	////声明为只读
	//var c3 <-chan int
	//<-c3
	//管道阻塞
	//c4 := make(chan int, 10)
	//for i := 0; i < 10; i++ {
	//	c4 <- i
	//}
	//stringChan := make(chan string, 5)
	//for i := 0; i < 5; i++ {
	//	stringChan <- "hello" + fmt.Sprintf("%d", i)
	//}
	//
	//for {
	//	select {
	//	case v := <-c4:
	//		fmt.Printf("从c4读取的数据%d\n", v)
	//	case v := <-stringChan:
	//		fmt.Printf("从stringChan读取的数据%s\n", v)
	//	default:
	//		fmt.Println("都取不到了，不玩了")
	//		return
	//	}
	//}
	//错误处理机制
	go testPainc()
}
